package com.aceleeyy.apb.base;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 消费者-基础控制器
 *
 * @author Ace Lee
 * @version 1.0
 * @date 2020/8/10 10:34
 **/
public abstract class BaseController<T extends BaseModel> {

    @Autowired
    private BaseService<T> baseService;

    /**
     * 列表
     *
     * @return
     */
    @ApiOperation("列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "isPage",value = "是否分页",dataType = "Boolean",example = "true"),
            @ApiImplicitParam(name = "attributes",value = "显示属性",dataType = "String",example = "id,createTime,creator"),
            @ApiImplicitParam(name = "filters",value = "过滤条件",dataType = "String",example = "id eq 3 and createTime ge '2020-08-17 11:00:00'"),
            @ApiImplicitParam(name = "orders",value = "排序",dataType = "String",example = "id asc,createTime desc"),
            @ApiImplicitParam(name = "skip",value = "跳过数量",dataType = "int",example = "0"),
            @ApiImplicitParam(name = "top",value = "每页数量",dataType = "int",example = "10")
    })
    @GetMapping("")
    public R getList(@RequestParam(value = "isPage", required = false) Boolean isPage,
                     @RequestParam(value = "attributes", required = false) String attributes,
                     @RequestParam(value = "filters", required = false) String filters,
                     @RequestParam(value = "orders", required = false) String orders,
                     @RequestParam(value = "skip", required = false) Integer skip,
                     @RequestParam(value = "top", required = false) Integer top) {
        return R.custom().success(baseService.getList(uri(),isPage,attributes,filters,orders,skip,top));
    }

    /**
     * 查看
     *
     * @param id
     * @return
     */
    @ApiOperation("查看")
    @ApiImplicitParam(name = "id",value = "主键",required = true,dataType = "Long")
    @GetMapping("/{id}")
    public R<T> getOne(@PathVariable Long id){
        return R.custom().success(baseService.getOne(uri(),id));
    }

    /**
     * 新增
     *
     * @param entity
     * @return
     */
    @ApiOperation("新增")
    @PostMapping("")
    public R<Boolean> addOne(@RequestBody T entity){
        return R.custom().success(baseService.addOne(uri(),entity));
    }

    /**
     * 修改
     *
     * @param id
     * @param entity
     * @return
     */
    @ApiOperation("修改")
    @ApiImplicitParam(name = "id",value = "主键",required = true,dataType = "Long")
    @PutMapping("/{id}")
    public R<Boolean> editOne(@PathVariable Long id,@RequestBody T entity){
        return R.custom().success(baseService.editOne(uri(),id,entity));
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation("删除")
    @ApiImplicitParam(name = "id",value = "主键",required = true,dataType = "Long")
    @DeleteMapping("/{id}")
    public R<Boolean> delOne(@PathVariable Long id){
        return R.custom().success(baseService.delOne(uri(),id));
    }

    /**
     * 服务者接口路径
     *
     * @return
     */
    protected abstract String uri();
}
